package com.xy.Servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xy.dao.impl.PageResults;
import com.xy.model.Admin;
import com.xy.model.Cell;
import com.xy.model.StockMove;
import com.xy.model.StockReceive;
import com.xy.model.StockSelect;
import com.xy.model.TextBook;
import com.xy.service.StockMoveService;
import com.xy.service.impl.CellServiceImpl;
import com.xy.service.impl.StockMoveServiceImpl;
import com.xy.service.impl.StockReceiveServiceImpl;
import com.xy.service.impl.StockServiceImpl;
import com.xy.service.impl.TextBookServiceImpl;

/**
 * Servlet implementation class StockMoveServlet
 */
@WebServlet("/stockMoveServlet")
public class StockMoveServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public StockMoveServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		StockMoveService service = new StockMoveServiceImpl();
		
		if ("list".equals(request.getParameter("status"))) {
			int currentPage = 0;
		      if (request.getParameter("goalpage") == null) {
		        currentPage = Integer.valueOf(request.getParameter("currentPage")) - 1;
		      } else
		        currentPage = Integer.valueOf(request.getParameter("goalpage")) - 1;

		      PageResults pageResults = new StockReceiveServiceImpl().listByPage(currentPage);

		      request.setAttribute("pageResults", pageResults);
		      request.setAttribute("status", "list");
		      request.setAttribute("list", null);

		      request.getRequestDispatcher("/pages/storage/storage005/storage_move_query.jsp").forward(request,
		          response);
		}else if ("query".equals(request.getParameter("status"))) {
			int currentPage = 0;
			if (request.getParameter("goalpage") == null) {
				currentPage = Integer.valueOf(request.getParameter("currentPage")) - 1;
			} else
				currentPage = Integer.valueOf(request.getParameter("goalpage")) - 1;

			String cell = null;
			String textBook = null;
			String list = null;

			if (request.getParameter("list") == null) {
				cell = request.getParameter("cell") == null ? "%" : request.getParameter("cell");
				textBook = request.getParameter("textBook") == null ? "%" : request.getParameter("textBook");
				list = cell + "," + textBook;
			} else {
				String s = new String(request.getParameter("list").getBytes("iso-8859-1"), "utf-8");
				String [] ss = s.split(",");
				for(int i=0;i<ss.length;i++) {
					cell = ss[0];
					textBook = ss[1];
				}
				list = cell + "," + textBook;
			}

			PageResults pageResults = new StockReceiveServiceImpl().queryFromMove(cell,textBook, currentPage);

			request.setAttribute("pageResults", pageResults);
			request.setAttribute("status", "query");
			request.setAttribute("list", list);

			request.getRequestDispatcher("/pages/storage/storage005/storage_move_query.jsp").forward(request,
			          response);
		}else if ("gomove".equals(request.getParameter("status"))) {
			String [] choice = request.getParameterValues("checkbox");
			List<StockReceive> st = new ArrayList();
			String name = "";
			String cell = "";
			String account = "";
			for(int i=0;i<choice.length;i++) {
				StockReceive receive = (StockReceive) new StockReceiveServiceImpl().get(Integer.valueOf(choice[i]));
				st.add(receive);
				if(i!=choice.length-1) {
					name += receive.getTextBook().getName() + ";";
					cell += receive.getTextBook().getCell().getName() + ";";
					account += receive.getAmount() + ";";
				}
				else {
					name += receive.getTextBook().getName();
					cell += receive.getTextBook().getCell().getName();
					account += receive.getAmount();
				}
			}
			request.getSession().setAttribute("stockReceives", st);
			request.setAttribute("name", name);
			request.setAttribute("cell", cell);
			request.setAttribute("account", account);
			
			request.getRequestDispatcher("/pages/storage/storage005/storage_move.jsp").forward(request,
			          response);
		}else if ("move".equals(request.getParameter("status"))) {
			List<StockReceive> stockReceives = (List<StockReceive>) request.getSession().getAttribute("stockReceives");
			String cell = request.getParameter("cell");
			String amount = request.getParameter("amount");
			for(int i=0;i<stockReceives.size();i++) {
				StockMove sm = new StockMove();
				StockReceive receive = stockReceives.get(i);
				TextBook tb = receive.getTextBook();
				Cell cell1 = new CellServiceImpl().get("name", cell);
				StockSelect ss = new StockServiceImpl().get("cell.name", cell);
				ss.setAmount(Integer.valueOf(amount));
				ss.setCell(cell1);
				ss.setTextBook(tb);
				cell1.getStockSelect().add(ss);
				new CellServiceImpl().update(cell1);
				new StockServiceImpl().update(ss);
				
				sm.setAdmin((Admin) request.getSession().getAttribute("admin"));
				sm.setAmount(Integer.valueOf(amount));
				sm.setDateTime(new Date());
				sm.setTextBook(tb);
				sm.setOcell(tb.getCell());
				sm.setNcell(cell1);
				service.add(sm);
				tb.setCell(cell1);
				new TextBookServiceImpl().update(tb);
				receive.setAmount(Integer.valueOf(amount));
				new StockReceiveServiceImpl().update(receive);
				
				
			}
			
			request.getRequestDispatcher("/stockMoveServlet?status=list&currentPage=0").forward(request,
			          response);
		}
	}

}
